<template>
  <div class="aa-container" style="padding-top: 20px;">
    <div class="flex-row" v-for="(item, index) in cominfo" :key="index">
      <div class="material-textfield green flex-all"  style="margin-right: 10px;"
        :class="{'validate-err': errors.has('cominfo_username_' + index)}">
        <input type="text" required v-model="item.username"  v-validate="'required|max:10'" :data-vv-name="'cominfo_username_' + index">
        <label data-content="姓名*"></label>
      </div>
      <div class="material-textfield green flex-all"  style="margin-right: 20px;"
        :class="{'validate-err': errors.has('cominfo_position_' + index)}">
        <input type="text" required v-model="item.position" v-validate="'required|max:10'" :data-vv-name="'cominfo_position_' + index"/>
        <label data-content="职务/职称*"></label>
      </div>
      <div class="material-textfield green flex-all"  style="margin-right: 50px;"
        :class="{'validate-err': errors.has('cominfo_specialty_' + index)}">
        <input type="text" required v-model="item.specialty" v-validate="'required|max:10'" :data-vv-name="'cominfo_specialty_' + index"/>
        <label data-content="专业/专长*"></label>
      </div>
      <div class="material-textfield green flex-all" style="margin-right: 100px;"
        :class="{'validate-err': errors.has('cominfo_responsibility_' + index)}">
        <input type="text" required v-model="item.responsibility" v-validate="'required|max:10'" :data-vv-name="'cominfo_responsibility_' + index"/>
        <label data-content="在项目中所起的作用*"></label>
      </div>
      <i class="el-icon-delete delete-info" @click="deleteItem(item, index)" v-show="cominfo.length > 1"></i>
    </div>
    <div class="add-info" @click="addItem()" v-show="cominfo.length < 12">
      <i class="el-icon-plus"></i>
      <span>添加</span>
    </div>
  </div>
</template>

<script>
  export default {
    title: '完成人情况表',
    name: 'com-info',
    data () {
      return {
        cominfo: [],
      }
    },
    props: {
      data: {}
    },
    mounted () {
      this.cominfo = this.data
      if (this.cominfo.length === 0) {
        this.addItem()
      }
    },
    methods: {
      addItem () {
        this.cominfo.push({
          username: '', // 姓名
          position: '', // 职务
          specialty: '', // 专业特长
          responsibility: '' // 主要责任说明
        })
      },
      deleteItem (item, index) {
        this.cominfo.splice(index, 1)
      },
      validate () {
        return this.$validator.validateAll()
      },
    },
    watch: {
      'cominfo': {
        handler: function (val, oldVal) {
          this.$emit('update:data', this.cominfo)
        },
        deep: true
      },
      'data': {
        handler: function (val, oldVal) {
          this.cominfo = val
          if (this.cominfo.length === 0) {
            this.addItem()
          }
        },
        deep: true
      }
    }
  }
</script>
